<?php
/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 */


/**
 * Packet handling when receiving data from
 * Camelot .NET Connector
 * @author Bendsoft
 */
class PacketReceiver extends CamelotDump {

    /**
     * Class constructor
     */
    function  __construct() {
    }

    /**
     * Function to insert packages
     *
     * @access	public
     * @param string $packet
     * @return	string|int if int above 0 then you have a success
     */
    function insert_packet($packet){

        # Retrieve checksum
        $packet_checksum = $this->get_checksum($packet);

        # Check if checksum exist
        $packet_checksum_check = $this->check_if_exist($packet_checksum);

        # If the checksum exist, return Error:100 (Packet exists)
        if($packet_checksum_check!=0)
            return 'msg:100';

        parent::setCamelotDumpDate(constant('NOW'));
        parent::setCamelotDumpIp(constant('IP'));
        parent::setCamelotDumpPacket($packet);
        parent::setCamelotDumpPacketMd5($packet_checksum);
        $new_id = parent::save('');

        return $new_id;
        
    }


    /**
     * Function to render and/or retrieve checksums
     *
     * @access	private
     * @param string|int $in_data
     * @param string $type
     * @return	string
     */
    private function get_checksum($in_data, $type="md5") {

        # if it's an int we assume it's an id and open the camelot_dump table
        if (is_numeric($in_data)) {
            //$dumpArray = parent::open($in_data);
            //$in_data = $dumpArray[0]['camelot_dump_packet'];
            parent::open($in_data);
            $in_data = parent::getCamelotDumpPacket();
        }

        # if the expression is "last" we obtain the last inserted record
        if ($in_data == "last") {
            //$dumpArray = parent::open("ORDER BY camelot_dump_id DESC LIMIT 1");
            //$in_data = $dumpArray[0]['camelot_dump_packet'];
            parent::open("ORDER BY camelot_dump_id DESC LIMIT 1");
            $in_data = parent::getCamelotDumpPacket();
        }

        switch ($type) {
            case "md5":
                $checksum = md5($in_data);
                break;
            case "crc32":
                $checksum = crc32($in_data);
                break;
            case "sha1":
                $checksum = sha1($in_data);
                break;
        }

        return $checksum;
    }

    /**
     * Function to compare checksums with stated and/or last insert
     *
     * @access	public
     * @param int $id1
     * @param int|string $id2
     * @return	boolean
     */
    function compare_checksums($id1, $id2 = "last") {

        $current = $this->get_checksum($id1);
        $last = $this->get_checksum($id2);

        # if same answer true, else false
        $ret = ($current == $last) ? true : false;

        return $ret;
    }

    /**
     * Function to check if a checksum exist
     *
     * @access	public
     * @param string $packet_checksum
     * @return	int Returns 0 if post dont exist, an other value would be the id
     * of the post with the inserted checksum
     */
    function check_if_exist($packet_checksum) {
        $res = parent::open("WHERE camelot_dump_packet_md5 ='$packet_checksum'");
        return $res;
    }
}
?>
